package cn.tongji.edu.tjmobile;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class EndCampusSelectionActivity extends Activity {

    private String campus;
    private List<View> componentList = new ArrayList<>();

    private void setStartCampusSelected(View component) {
        RelativeLayout itemLayout = (RelativeLayout) component.findViewById(R.id.item_layout);
        TextView startCampusTextView = (TextView) component.findViewById(R.id.start_campus_textview);
        startCampusTextView.setTextColor(Color.parseColor("#fffa9d4c"));
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View imageLayout = inflater.inflate(R.layout.component_selected_image_layout, null);
        itemLayout.addView(imageLayout);
    }

    private void removeStartCampusSelected(View component) {
        RelativeLayout itemLayout = (RelativeLayout) component.findViewById(R.id.item_layout);
        TextView startCampusTextView = (TextView) component.findViewById(R.id.start_campus_textview);
        startCampusTextView.setTextColor(Color.parseColor("#ff000000"));
        itemLayout.removeViewAt(1);
    }

    private boolean isStartCampusSelected(View component) {
        TextView startCampusTextView = (TextView) component.findViewById(R.id.start_campus_textview);
        int color = startCampusTextView.getCurrentTextColor();
        if(color == -352948){
            return true;
        }
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_end_campus_selection);

        Intent intent = getIntent();
        final String startCampus = intent.getStringExtra("startCampus");
        final List<String> endCampusOptions = intent.getStringArrayListExtra("endCampusOptions");
        final String json = intent.getStringExtra("json");

        TextView backView = (TextView) findViewById(R.id.back_view);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread () {
                    public void run () {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });

        TextView nextStepView = (TextView) findViewById(R.id.next_step_view);
        nextStepView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent();
                    JSONArray jsonArray = new JSONArray(json);
                    String routeId = "";

                    for(int i = 0;i != componentList.size();i++){
                        if(isStartCampusSelected(componentList.get(i))){
                            TextView textView = (TextView) componentList.get(i).findViewById(R.id.start_campus_textview);
                            campus = textView.getText().toString();
                            System.out.print(campus);
                        }
                    }

                    for(int i = 0;i != jsonArray.length();i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String start = jsonObject.getString("start");
                        String end = jsonObject.getString("end");
                        if(start.equals(startCampus) && end.equals(campus)){
                            routeId = jsonObject.getString("route_id");
                        }
                    }
                    intent.putExtra("routeId", routeId);
                    intent.putExtra("start", startCampus);
                    intent.putExtra("end", campus);
                    intent.setClass(EndCampusSelectionActivity.this, BusTimelineSelectionActivity.class);
                    startActivity(intent);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

        LinearLayout containerLayout = (LinearLayout) findViewById(R.id.end_campus_container_layout);
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        for(int i = 0;i != endCampusOptions.size();i++){
            final View component = inflater.inflate(R.layout.component_start_campus_layout, null);
            TextView startCampusTextView = (TextView) component.findViewById(R.id.start_campus_textview);
            final String label = endCampusOptions.get(i);
            startCampusTextView.setText(label);
            containerLayout.addView(component);
            if(i == 0){
                setStartCampusSelected(component);
            }
            componentList.add(component);
            component.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!isStartCampusSelected(component)){
                        setStartCampusSelected(component);
                    }
                    for(int i = 0;i != componentList.size(); i++){
                        if(isStartCampusSelected(componentList.get(i)) && !component.equals(componentList.get(i))){
                            removeStartCampusSelected(componentList.get(i));
                        }
                    }
                    campus = label;
                }
            });
        }
    }
}
